package com.sx.water.modules.sever.impl;

import com.sx.water.modules.entity.IntegerTimeRainingBo;
import com.sx.water.modules.sever.RainQuartzServer;
import com.sx.water.modules.sever.SurfaceRainfallServer;
import com.sx.water.util.TableUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 定时任务实现类
 * @author: taoxi
 * @create: 2018-11-18 17:49
 **/
@Service("RainQuartzServer")
public class RainQuartzImpl implements RainQuartzServer {

    @Autowired
    SurfaceRainfallServer surfaceRainfallServer;

    /**
     * @param pr01 阀值
     * @param num   1/3/6
     * @return
     */
    @Override
    public Map<String, Object> RainFallPre1(String pr01,int num) {
        Map<String, Object> map = new HashMap<>();
        //获取最新时间 yymmddHH
        String time = surfaceRainfallServer.selectMaxtime();
        IntegerTimeRainingBo integerTimeRainingBo = getIntegerTimeRainingBo(pr01, num, time);
        //降水统计时段 站点 超过阀值
        List<Map<String, Object>> listpre2 = surfaceRainfallServer.selectGetRainInfoQuartz(integerTimeRainingBo);
        map.put("time", time);
        map.put("rain", listpre2);
        return map;
    }



    /**
     * @param param 阀值
     * @param num   1/3/6
     * @return
     */
    private IntegerTimeRainingBo getIntegerTimeRainingBo(String param, int num, String time) {
        // String time = surfaceRainfallServer.selectMaxtime();
        IntegerTimeRainingBo integerTimeRainingBo = new IntegerTimeRainingBo();
        integerTimeRainingBo.setCountry("青田");
        //设置阀值
        integerTimeRainingBo.setThresholdRain(param);

        Map<String, String> map = TableUtil.getTimeComparison(num, time);
        integerTimeRainingBo.setStartTime(map.get("startTime"));
        integerTimeRainingBo.setEndTime(map.get("endTime"));
        integerTimeRainingBo.setDataBasesTimeParameter(map.get("tableSuffix"));
        //判断是否 时间间隔在同一张表
        if (map.get("tableSuffix2") != null) {
            //不是同一张表
            integerTimeRainingBo.setDataBasesTimeParameterBef(map.get("tableSuffix2"));
            StringBuffer stringBuffer = new StringBuffer();
            //动态语句  BETWEEN'18100100' and  '18103018'
            stringBuffer.append("BETWEEN").append(" ").append(map.get("startTime2")).append(" ").append("AND").append(" ").append(map.get("endTime2"));
            integerTimeRainingBo.setAutoStationAddSql(stringBuffer.toString());
        }

        return integerTimeRainingBo;
    }



}
